//
//  SwitchViewController.m
//  FreeSWITCH_Console
//
//  Created by Christopher Danielson on 4/6/09.
//  Copyright 2009 MaxPowerSoft, LLC. All rights reserved.
//

#import "UserSetting.h"
#import "SwitchViewController.h"
#import "MainMenuViewController.h"
#import "EditSettingsViewController.h"
#import "FunctionSettingsViewController.h"
#import "ConsoleViewController.h"

@implementation SwitchViewController

@synthesize mainMenuViewController;
@synthesize mainMenuNavigationBar;
@synthesize mainMenuToolBar;
@synthesize editSettingsViewController;
@synthesize functionSettingsViewController;
@synthesize consoleViewController;
@synthesize currentMode;
@synthesize userSetting;
//@synthesize consoleToolBar;
@synthesize consoleNavBar;

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

#define ANIMATION_DURATION	1.00

- (IBAction) toggleDelete {
	[self.mainMenuViewController toggleEdit:nil];
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	currentMode = MODE_MAINMENU;
	//Initialize and load our Main Menu Controller
	MainMenuViewController *mainMenuController = [[MainMenuViewController alloc]
												  initWithNibName:@"MainMenuView" bundle:nil];
	self.mainMenuViewController = mainMenuController;
	[mainMenuController release];
	[self.mainMenuViewController setSwitchViewController:self];
	
	UINavigationBar *aNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
	aNavigationBar.barStyle = UIBarStyleDefault;
	self.mainMenuNavigationBar = aNavigationBar;
	[aNavigationBar release];
	
	UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarStyleDefault target:self action:@selector(toggleDelete)];
	UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"FreeSWITCH Console"];
	navigationItem.rightBarButtonItem = buttonItem;
	[mainMenuNavigationBar pushNavigationItem:navigationItem animated:NO];
	[navigationItem release];
	[buttonItem release];

	UIToolbar *toolBarItem = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 416.0, 320.0, 44.0)];
	toolBarItem.barStyle = UIBarStyleDefault;
	self.mainMenuToolBar = toolBarItem;
	[toolBarItem release];
	
	[self.view insertSubview:self.mainMenuViewController.view atIndex:0];
	[mainMenuViewController.view insertSubview:mainMenuNavigationBar aboveSubview:mainMenuViewController.view];
	[mainMenuViewController.view insertSubview:mainMenuToolBar aboveSubview:mainMenuViewController.view];
	
    [super viewDidLoad];
}

//- (IBAction)settingsView:(id)sender {
- (IBAction)settingsView {
	currentMode = MODE_SETTINGS;
	if (editSettingsViewController.view == nil) {
		EditSettingsViewController *editSettingsController = [[EditSettingsViewController alloc]
															  initWithNibName:@"EditSettingsView" bundle:nil];
		self.editSettingsViewController = editSettingsController;
		[editSettingsController release];
		[self.editSettingsViewController setSwitchViewController:self];
	} else {
		[self.editSettingsViewController resetTable];
	}
	//remove the current view from super
	[UIView beginAnimations:@"View Flip" context:nil];
	[UIView setAnimationDuration:ANIMATION_DURATION];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
	[self.editSettingsViewController viewWillAppear:YES];
	[self.mainMenuViewController viewWillAppear:YES];
	[self.mainMenuViewController.view removeFromSuperview];
	[self.view insertSubview:editSettingsViewController.view atIndex:0];
	[self.mainMenuViewController viewDidDisappear:YES];
	[self.editSettingsViewController viewDidAppear:YES];
	[UIView commitAnimations];
}

- (void) mainMenuFromSettingsView {
	currentMode = MODE_MAINMENU;
	[UIView beginAnimations:@"View Flip" context:nil];
	[UIView setAnimationDuration:ANIMATION_DURATION];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
	[self.mainMenuViewController viewWillAppear:YES];
	[self.editSettingsViewController viewWillAppear:YES];
	[self.editSettingsViewController.view removeFromSuperview];
	[self.view insertSubview:mainMenuViewController.view atIndex:0];
	[self.editSettingsViewController viewDidDisappear:YES];
	[self.mainMenuViewController viewDidAppear:YES];
	[UIView commitAnimations];
}

- (IBAction)functionSettingsView:(id)sender {
	if (functionSettingsViewController.view == nil) {
		FunctionSettingsViewController *funcSettingsController = [[FunctionSettingsViewController alloc] 
																  initWithNibName:@"FunctionSettingsView" bundle:nil];
		self.functionSettingsViewController = funcSettingsController;
		[funcSettingsController release];
	}
	[mainMenuViewController.view removeFromSuperview];
	[self.view insertSubview:functionSettingsViewController.view atIndex:0];
}

- (IBAction)consoleViewSendEmail {
	NSString *str = [[[NSString alloc] initWithFormat:@"mailto:?to=&subject=FreeSWITCH&body=%@", 
					 [consoleViewController.consoleTextView.text stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]] autorelease];
	NSURL *url = [[[NSURL alloc] initWithString:str] autorelease];
	[[UIApplication sharedApplication] openURL:url];
}

- (IBAction)mainMenuFromConsoleView {
	[consoleViewController stop];
	currentMode = MODE_MAINMENU;
	[UIView beginAnimations:@"View Flip" context:nil];
	[UIView setAnimationDuration:ANIMATION_DURATION];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
	[self.mainMenuViewController viewWillAppear:YES];
	[self.consoleViewController viewWillAppear:YES];
	[self.consoleViewController.view removeFromSuperview];
	[self.view insertSubview:mainMenuViewController.view atIndex:0];
	[self.consoleViewController viewDidDisappear:YES];
	[self.mainMenuViewController viewDidAppear:YES];
	[UIView commitAnimations];
}

- (IBAction)consoleView {
	currentMode = MODE_CONSOLE;
	if (consoleViewController == nil) {
		ConsoleViewController *consoleController = [[ConsoleViewController alloc]
													initWithNibName:@"ConsoleView" bundle:nil];
		self.consoleViewController = consoleController;
		[consoleController release];
		
		UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(mainMenuFromConsoleView)];
		
		UIBarButtonItem *emailButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Email" style:UIBarButtonItemStyleBordered target:self action:@selector(consoleViewSendEmail)];

		UINavigationBar *aNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 416.0, 320.0, 44.0)];
		aNavigationBar.barStyle = UIBarStyleDefault;
		self.consoleNavBar = aNavigationBar;
		[aNavigationBar release];
		UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
		navigationItem.rightBarButtonItem = emailButtonItem;
		navigationItem.leftBarButtonItem = backButtonItem;
		[consoleNavBar pushNavigationItem:navigationItem animated:NO];
		[navigationItem release];
		
		/*
		UIToolbar *toolBarItem = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 416.0, 320.0, 44.0)];
		toolBarItem.barStyle = UIBarStyleDefault;
		[toolBarItem setItems:[NSArray arrayWithObjects:backButtonItem,emailButtonItem,nil]];
		self.consoleToolBar = toolBarItem;
		[toolBarItem release];
		*/
		[backButtonItem release];
		[emailButtonItem release];

		[self.view insertSubview:self.mainMenuViewController.view atIndex:0];
		//[consoleViewController.view insertSubview:consoleToolBar aboveSubview:consoleViewController.view];
		[consoleViewController.view insertSubview:consoleNavBar aboveSubview:consoleViewController.view];
	}

	if (self.userSetting != nil)
		[consoleViewController setUserSetting:self.userSetting];
	else
		return;
	
	[UIView beginAnimations:@"View Flip" context:nil];
	[UIView setAnimationDuration:ANIMATION_DURATION];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
	[self.consoleViewController viewWillAppear:YES];
	[self.mainMenuViewController viewWillAppear:YES];
	[self.mainMenuViewController.view removeFromSuperview];
	[self.view insertSubview:consoleViewController.view atIndex:0];
	[self.mainMenuViewController viewDidDisappear:YES];
	[self.consoleViewController viewDidAppear:YES];
	[UIView commitAnimations];
	
	[consoleViewController start];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
	if (currentMode == MODE_CONSOLE)
		return YES;
	else
		return NO;
}

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration {
	UIInterfaceOrientation toOrientation = self.interfaceOrientation;
	[UIView beginAnimations:@"move code" context:nil];

	//CGRect rect;
	if (toOrientation == UIInterfaceOrientationPortrait || toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
		if (currentMode == MODE_CONSOLE) {
			self.consoleViewController.consoleTextView.frame = CGRectMake(0.0, 0.0, 320.0, 390.0);
			self.consoleViewController.consoleInputField.frame = CGRectMake(0.0, 390.0, 320.0, 26.0);
		}
	} else {
		if (currentMode == MODE_CONSOLE) {
			self.consoleViewController.consoleTextView.frame = CGRectMake(0.0, 0.0, 480.0, 275.0);
			self.consoleViewController.consoleInputField.frame = CGRectMake(0.0, 275.0, 480.0, 40.0);
		}
	}	
	[UIView commitAnimations];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
	//[consoleToolBar release];
	[consoleNavBar release];
	[mainMenuNavigationBar release];
	[mainMenuToolBar release];
	[mainMenuViewController release];
	[editSettingsViewController release];
	[functionSettingsViewController release];
	[consoleViewController release];
    [super dealloc];
}

- (void)setUserSetting:(UserSetting *)userSetObj {
	userSetting = userSetObj;
}

@end
